package com.zhsj.OkeHomeConsumption.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import android.util.Log;

import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

public class SharedPreferanceUtils {
    private SharedPreferences sharedPreferences = null;
    private SharedPreferences.Editor editor = null;
    private int openCount = 0;
    private int contract = 0;
    private String shared_preferences_flag = null;
    private String shared_openCount_flag = null;

    /**
     * 构造方法
     *
     * @param shared_preferences_flag
     * @param context
     */
    public SharedPreferanceUtils(String shared_preferences_flag, Context context) {
        this(context);
        this.shared_preferences_flag = shared_preferences_flag;
        editor = sharedPreferences.edit();
    }

    /**
     * 构造方法
     *
     * @param context
     */
    public SharedPreferanceUtils(Context context) {
        if (context != null) {
            sharedPreferences = context.getSharedPreferences(
                    Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
            editor = sharedPreferences.edit();
        }
    }

    /**
     * @param @param netUserInfo
     * @return void    返回类型
     * @throws
     * @Title: setSaveUserInfo
     * @Description: TODO 保存用户信息
     */
    public void setSaveUserInfo(FormalUserInfo netUserInfo) {
//        Gson gson = new Gson();
//        String json = gson.toJson(netUserInfo);
//        editor.putString(Constants.SHARED_PERFERENCE_USERINFO, json);

//        // 创建字节输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            // 创建对象输出流，并封装字节流
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            // 将对象写入字节流
            oos.writeObject(netUserInfo);
            // 将字节流编码成base64的字符窜
            String netUserInfo_Base64 = new String(Base64.encodeToString(baos
                    .toByteArray(), Base64.DEFAULT));
            editor.putString(Constants.SHARED_PERFERENCE_USERINFO, netUserInfo_Base64);
            editor.commit();
        } catch (Exception e) {
            // TODO Auto-generated
        }
        Log.i("ok", "存储成功");
    }

    /**
     * @param @return
     * @return UserInfo    返回类型
     * @throws
     * @Title: getUserInfo
     * @Description: TODO 获取用户信息
     */
    public FormalUserInfo getUserInfo() {
//        Gson gson = new Gson();
//        String json = sharedPreferences.getString(Constants.SHARED_PERFERENCE_USERINFO, null);
//        Type type = new TypeToken<FormalUserInfo>() {
//        }.getType();
//
//        FormalUserInfo netUserInfo = gson.fromJson(json,type);


        FormalUserInfo netUserInfo = null;
        String productBase64 = sharedPreferences.getString(Constants.SHARED_PERFERENCE_USERINFO, "");
        //读取字节
        byte[] base64 = Base64.decode(productBase64.getBytes(), Base64.DEFAULT);

        //封装到字节流
        ByteArrayInputStream bais = new ByteArrayInputStream(base64);
        try {
            //再次封装
            ObjectInputStream bis = new ObjectInputStream(bais);
            try {
                //读取对象
                netUserInfo = (FormalUserInfo) bis.readObject();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (StreamCorruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return netUserInfo;
    }

    /**
     * @param @param token
     * @return void    返回类型
     * @throws
     * @Title: setToken
     * @Description: TODO 存储token
     */
    public void setToken(String token) {
        editor.putString(Constants.SHARED_PERFERENCE_TOKEN,
                token);
        editor.commit();
    }

    /**
     * @param @return
     * @return String    返回类型
     * @throws
     * @Title: getToken
     * @Description: TODO 获取token
     */
    public String getToken() {
        return sharedPreferences.getString(
                Constants.SHARED_PERFERENCE_TOKEN, "");
    }

    public void setnumber(String number) {
        editor.putString("number", number);
        editor.commit();
    }


    public String getnumber() {
        return sharedPreferences.getString("number",
                "");

    }

    /**
     * @param @param isLogin
     * @return void    返回类型
     * @throws
     * @Title: setIsLogin
     * @Description: TODO 设置是否登录的标识
     */
    public void setIsLogin(String isLogin) {
        editor.putString(Constants.SHARED_PREFERENCE_ISLOGIM, isLogin);
        editor.commit();
    }


    /**
     * @param @return
     * @return String    返回类型
     * @throws
     * @Title: getIsLogin
     * @Description: TODO 获取是否登录的标识
     */
    public String getIsLogin() {
        return sharedPreferences.getString(Constants.SHARED_PREFERENCE_ISLOGIM,
                "");
    }
    /**
     * 保存 DeviceToken
     *
     * @param
     * @return void
     * 建时间：2017/1/13 17:33
     */
    public void setDeviceToken(String deviceToken) {
        editor.putString(Constants.SHARED_PERFERENCE_DEVICETOKEN,
                deviceToken);
        editor.commit();
    }

    /**
     * 获取 DeviceToken
     *
     * @param
     * @return String
     * 创建间：2017/1/13 17:34
     */
    public String getDeviceToken() {
        return sharedPreferences.getString(
                Constants.SHARED_PERFERENCE_DEVICETOKEN, "");
    }

    /**
     * 设置 RongyunToken
     *
     * @param rongyunToken
     * @return void
     * 创建间：2017/2/14 9:56
     */
    public void setRongyunToken(String rongyunToken) {
        editor.putString(Constants.SHARED_PERFERENCE_RONGYUNTOKEN,
                rongyunToken);
        editor.commit();
    }

    /**
     * @param @param selectcity
     * @return void    返回类型
     * @throws
     * @Title: setSelectCity
     * @Description: TODO 存储已选择城市最小颗粒度
     */
    public void setSelectCity(String selectcity) {
        editor.putString(Constants.SHARED_PERFERENCE_SELECTCITY,
                selectcity);
        editor.commit();
    }

    /**
     * @param @return
     * @return String    返回类型
     * @throws
     * @Title: getSelectCity
     * @Description: TODO 获取已选择的城市最小颗粒度
     */
    public String getSelectCity() {
        return sharedPreferences.getString(
                Constants.SHARED_PERFERENCE_SELECTCITY, "");
    }

    /**
     * @param @param isLogin
     * @return void    返回类型
     * @throws
     * @Title: setIsLogin
     * @Description: TODO 存贮市县乡
     */
    public void setcityName(String cityName) {
        editor.putString("cityName", cityName);
        editor.commit();
    }


    /**
     * @param @return
     * @return String    返回类型
     * @throws
     * @Title: getIsLogin
     * @Description: TODO 存贮市县乡
     */
    public String getcityName() {
        return sharedPreferences.getString("cityName",
                "");
    }

    /**
     * @throws
     * @描述:
     * @方法名: 设置省份ID
     * @返回类型
     * @修改备注
     */
    public void setProvinceId(int provinceId) {
        editor.putInt(Constants.SHARED_PERFERENCE_PROVINCEID,
                provinceId);
        editor.commit();
    }

    /**
     * @throws
     * @描述:获取省份ID
     * @方法名:
     * @返回类型
     * @修改备注
     */
    public int getProvinceId() {
        return sharedPreferences.getInt(
                Constants.SHARED_PERFERENCE_PROVINCEID, 0);
    }


    /**
     * @throws
     * @描述:设置城市ID
     * @方法名:
     * @返回类型
     * @修改备注
     */
    public void setSelectCityId(int selectCityId) {
        editor.putInt(Constants.SHARED_PERFERENCE_SELECTCITYID,
                selectCityId);
        editor.commit();
    }

    /**
     * @throws
     * @描述:获取城市ID
     * @方法名:
     * @返回类型
     * @修改备注
     */
    public int getSelectCityId() {
        return sharedPreferences.getInt(
                Constants.SHARED_PERFERENCE_SELECTCITYID, 0);
    }

    /**
     * 存储用户身份
     */
    public void setlifeType(int lifeType) {
        editor.putInt("lifeType", lifeType);
        editor.commit();
    }


    public int getlifeType() {
        return sharedPreferences.getInt("lifeType",0);
    }

    /*
     * @Description: TODO 存储定位城市
     */
    public void setPositioningSelectCity(String selectcity) {
        editor.putString("positionselectcity",
                selectcity);
        editor.commit();
    }

    public String getPositioningSelectCity() {
        return sharedPreferences.getString(
                "positionselectcity", "");
    }

    /**
     * 获得打开的次数
     *
     * @return
     */
    public int getActivityOpenCount() {
        openCount = sharedPreferences.getInt(shared_preferences_flag, 0);
        return openCount;
    }


}
